﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace TextTools
{
    /// <summary>
    /// ContentComp.xaml 的交互逻辑
    /// </summary>
    public partial class ContentComp : Window
    {
        public class ContentInfo
        {
            public int fcFlag { get; set; }
            public string fcContent1 { get; set; }
            public string fcContent2 { get; set; }
        }
        public class ThinkData
        {
            public int flag { get; set; } //1：不相同； 0：相同
            public int lie1 { get; set; }  //文件1为置
            public int lie2 { get; set; }  //文件2对于相同位置
        }

        public ContentComp(string _file1="",string _file2="")
        {
            InitializeComponent();
            filename1 = _file1;
            filename2 = _file2;
            if (!string.IsNullOrWhiteSpace(filename1) && !string.IsNullOrWhiteSpace(filename2))
            {
                sDirText.Text = filename1;
                dDirText.Text = filename2;
                compAction(filename1, filename2);
            }
        }
        private string filename1 = "";
        private string filename2 = "";

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            filename1 = sDirText.Text;
            Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
            ofd.DefaultExt = "*.txt";
            ofd.Filter = "TextFile(*.txt)|*.txt|AllFile(*.*)|*.*";
            if (ofd.ShowDialog() == true)
            {
                filename1 = ofd.FileName;
                sDirText.Text = filename1;
            }
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            filename2 = dDirText.Text;
            Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
            ofd.DefaultExt = "*.txt";
            ofd.Filter = "TextFile(*.txt)|*.txt|AllFile(*.*)|*.*";
            if (ofd.ShowDialog() == true)
            {
                filename2 = ofd.FileName;
                dDirText.Text = filename2;
            }
        }

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {// 开始比较
            filename1 = sDirText.Text;
            filename2 = dDirText.Text;
            if (!string.IsNullOrWhiteSpace(filename1) && !string.IsNullOrWhiteSpace(filename2))
            {
                ShowInfo.Content = "开始比较分析...";
                compAction(filename1, filename2);
                ShowInfo.Content = "比较分析完成...";
            }
        }
        private bool compAction(string _file1, string _file2)
        {
            bool result = true;
            int lie1 = 0, lie2 = 0, lie3 = 0;
            List<string> fcontent1 = getfilecontent(_file1);
            List<string> fcontent2 = getfilecontent(_file2);
            int filenum1=fcontent1.Count();
            int filenum2=fcontent2.Count();
            List<ThinkData> ttds = new List<ThinkData>();
            foreach (string tstr in fcontent1)
            {
                ThinkData ttd = new ThinkData();
                ttd.lie1 = lie1;
                ttd.lie2 = lie2;
                ttd.flag=1;  //为1 不相同
                lie3 = lie2;
                for (; lie2 < filenum2; lie2++)
                {
                    if (tstr == fcontent2[lie2])
                    {
                        ttd.flag = 0;
                        ttd.lie2 = lie2;
                        break;
                    }
                }
                if (ttd.flag == 0) lie2 += 1;
                else lie2 = lie3;
                ttds.Add(ttd);
                lie1 += 1;
            }
            lie1 = 0; lie2 = 0;
            CompListView1.Items.Clear();
            int tdnum=ttds.Count();
            for (int i1 = 0; i1 < tdnum; i1++)
            {
                ContentInfo tci = new ContentInfo();
                tci.fcFlag = ttds[i1].flag;
                tci.fcContent1 = fcontent1[ttds[i1].lie1];
                if (tci.fcFlag == 0)  // 如果相同
                    tci.fcContent2 = fcontent2[ttds[i1].lie2];
                CompListView1.Items.Add(tci);

                if (i1 < tdnum - 1)
                {
                    if ((ttds[i1].lie2 + 1) < ttds[i1 + 1].lie2)
                    {
                        ContentInfo tci2 = new ContentInfo();
                        tci2.fcFlag = 1;
                        tci2.fcContent1 = "";
                        tci2.fcContent2 = fcontent2[ttds[i1].lie2+1];
                        for (int i2 = ttds[i1].lie2+2; i2 <= ttds[i1 + 1].lie2-1; i2++)
                        {
                            tci2.fcContent2 += "\n" + fcontent2[i2];
                        }
                        CompListView1.Items.Add(tci2);
                    }
                }

            }
            return result;
        }
        private List<string> getfilecontent(string tfn)
        {
            string[] strs = EncodingType.GetFileString(tfn).Split('\n');
            List<string> outstrs = new List<string>();
            string str;
            foreach (string tstr in strs)
            {
                str = tstr.Trim();
                str=str.Replace("\r","");
                if (string.IsNullOrWhiteSpace(str)) continue;
                outstrs.Add(str);
            }
            return outstrs;
        }
    }
}
